package com.QK.cnstudy.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.QK.cnstudy.R;
import com.QK.cnstudy.entity.Work;

public class WorkAdapter extends QKAdapter {

    private int resource = R.layout.work_item;

    public WorkAdapter(Context context, List<Map<String, Object>> list) {
        super(context, list);
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Map<String, Object> map = list.get(position);
        Work work = (Work) map.get("object");
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.titleTextView = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        String timeString;
        if (work.getOpenTimes() == 0) {
            timeString = "打开次数 " + work.getOpenTimes() + " 次";
        } else {
            DateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
            timeString = dateFormat.format(new Date(work.getLastOpentime())) + " 打开次数 " + work.getOpenTimes() + " 次";
        }
        holder.timeTextView.setText(timeString);
        holder.titleTextView.setText("" + work.getName() + "");
        return convertView;
    }

    class ViewHolder {
        public TextView timeTextView;
        public TextView titleTextView;
    }
}
